//
//  ClassificationController.m
//  SmallEasyLife
//
//  Created by qianfeng on 15/10/23.
//  Copyright © 2015年 GXK. All rights reserved.
//

#import "ClassificationController.h"
#import "ClassificationModel.h"
#import "TagsCell.h"
#import "Tags.h"
#import "TagsHeaderView.h"
#import "UIImageView+AFNetworking.h"
@interface ClassificationController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *classificationCollection;
@property (nonatomic, retain) NSArray *tagsArray;
@property (nonatomic, retain) NSArray *list;
@end

@implementation ClassificationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadDataFromCache];
}

- (void)loadDataFromCache
{
    NSString * path = [[NSBundle mainBundle] pathForResource:@"Classification" ofType:@"json"];
    NSData * data = [NSData dataWithContentsOfFile:path];
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",dic);
    self.list = dic[@"list"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = self.list[section][@"tags"];
    return array.count;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.list.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ClassificationCell";
    TagsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    Tags *tags = [Tags tagsWithDictionary:self.list[indexPath.section][@"tags"][indexPath.row]];
    NSLog(@"%@",tags);
    NSString *urlString = [tags.img stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlString];
    [cell.image setImageWithURL:url];
    cell.title.text = tags.name;
    return cell;
}

//头部显示的内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    TagsHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
    headerView.titleLabel.text = self.list[indexPath.section][@"title"];
    return headerView;
}


#pragma mark - Navigation

 //In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//     Get the new view controller using [segue destinationViewController].
//     Pass the selected object to the new view controller.
    
}


@end
